package controller;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Iterator;

import model.Item;
import model.Order;
import model.OrderContainer;
import controller.StorageController;

public class OrderController {

	private static OrderController orderController = null;
	private static OrderContainer orderContainer; 
	
	private OrderController(){
		orderContainer = OrderContainer.getInstance();
	}
	
	public static OrderController getInstance(){
		if(orderController == null){
			orderController = new OrderController();
		}
		return orderController;
	}
	
	public void addItemToOrder(int itemId, int orderId){
		Order o = orderContainer.findOrder(orderId);
		o.addItem(itemId, 1);
	}
	
	public ArrayList<String> getOrders(int id){
		return OrderContainer.getInstance().getOrders(id);
	}
	//int id, DateFormat date, boolean isPaid, String status, int OrderId	
	public String getId(int id){
//		return orderContainer.findOrder(id).xx;
		return null; /// mangler
	}
	
	public void setId(int id, int newId){
		orderContainer.findOrder(id).setId(newId);
	}

	public DateFormat getDate(int id){
		return orderContainer.findOrder(id).getDate();
	}
	
	public void setDate(int id){
		orderContainer.findOrder(id).setDate(DateFormat.getDateTimeInstance());
	}

	public boolean isPaid(int id){
		return orderContainer.findOrder(id).isPaid();
	}
	
	public void setPaid(int id, boolean isPaid){
		orderContainer.findOrder(id).setPaid(isPaid);
	}

	public String getStatus(int id){
		return orderContainer.findOrder(id).getStatus();
	}
	
	public void setStatus(int id, String status){
		orderContainer.findOrder(id).setStatus(status);
	}
	
	public int getOrderId(int id){
		return orderContainer.findOrder(id).getId();
	}
	
	public void setOrderId(int id, int orderId){
		orderContainer.findOrder(id).setId(orderId);
	}
	public int getCustomerId(int id) {
		return orderContainer.findOrder(id).getCustomerId();
	}
		
	public void setCustomerId(int id,int customerId) {
		orderContainer.findOrder(id).setCustomerId(customerId);
			
	}
	                              
	public ArrayList<Object[]> getItems(int orderId){
		ArrayList<Object[]> al = new ArrayList<Object[]>();
		Iterator it = orderContainer.findOrder(orderId).getHashMap().keySet().iterator();
		while(it.hasNext()){
			
			int itemId = (Integer)it.next();
			int itemAmount = orderContainer.findOrder(orderId).getHashMap().get(itemId);
			String itemName = StorageController.getInstance().getName(String.valueOf(itemId));
			Double itemPrice = StorageController.getInstance().getPrice(String.valueOf(itemId));
			System.out.println(itemId + " " + itemName + " " + itemAmount + " " + itemPrice);
			al.add(new Object[]{ itemId, itemName, itemAmount, itemPrice});
		}
		System.out.println("DFNKJDNBKJDB");
		for(Object[] o : al){
			System.out.println(" dsds " + o);
		}
		return al;
	}
	//** Creates an order object */
	public void createOrder(int id, boolean isPaid, String status, int customerId){
		orderContainer.addOrder(new Order(id, DateFormat.getDateTimeInstance(), isPaid, status, customerId));
	}
	
	//** Updates an order object */
	public void updateOrder(int id, DateFormat date, boolean isPaid, String status, int customerId){
		Order order = orderContainer.findOrder(id);
		order.setId(id);
		order.setDate(date);
		order.setPaid(isPaid);
		order.setStatus(status);
		order.setCustomerId(customerId);
	}
}
